package com.zxs.category.service.impl;

import com.zxs.category.mapper.CategoryMapper;
import com.zxs.category.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import parm.PageParam;
import parm.ProductHotParam;
import pojo.Category;
import utils.R;

import java.util.List;

/**
 * @author zxs
 * *7 18:21
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据类别名称,查询类别对象
     *
     * @param categoryName
     * @return
     */
    @Override
    public R byName(String categoryName) {

        //封装查询参数
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name",categoryName);
        //查询数据库
        Category category = categoryMapper.selectOne(categoryQueryWrapper);
        //结果封装
        if (category == null){
            log.info("CategoryServiceImpl.byName业务结束，结果:类别查询失败");
            return R.fail("类别查询失败!");
        }
        log.info("CategoryServiceImpl.byName业务结束，结果:{}","类别查询成功");
        return R.ok("类别查询成功!",category);
    }

    /**
     * 返回全部类别信息
     * @return
     */
    @Override
    public R list() {
        List<Category> categoryList = categoryMapper.selectList(null);
        return R.ok(categoryList);
    }

    @Override
    public R hots(ProductHotParam  productHotParam) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
        queryWrapper.in("category_name", productHotParam.getCategoryName());
        queryWrapper.select("category_id");
        //查询数据库
        List<Object> objs = categoryMapper.selectObjs(queryWrapper);

        return R.ok("类别集合查询成功",objs);
    }

    /**
     * 后台的分类列表
     * @param pageParam
     * @return
     */
    @Override
    public R categoryList(PageParam pageParam) {
        Page<Category> categoryPage = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        Page<Category> page = categoryMapper.selectPage(categoryPage, null);
        List<Category> records = page.getRecords();
        long total = page.getTotal();
        return R.ok("分类列表查询成功！",records, total);
    }

    /**
     * 后台添加类别
     * @param category
     * @return
     */
    @Override
    public R categorySave(Category category) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<Category>();
        queryWrapper.eq("category_name", category.getCategoryName());
        Long count = categoryMapper.selectCount(queryWrapper);
        log.info("是否存在：",count);
        if (count >0){
            return  R.fail("类别已经存在，请重新输入！");
        }
        int insert = categoryMapper.insert(category);
        log.info("CategoryServiceImpl.byName业务结束，结果:",insert);
        return R.ok("类别添加成功");
    }

    /**
     * 输出某个类别
     * @param category
     * @return
     */
    @Override
    public R categoryRemove(Category category) {
        int deleteById = categoryMapper.deleteById(category.getCategoryId());
        if (deleteById == 0){
            return  R.fail("类别删除失败！");
        }
        return R.ok("类别删除成功！");
    }

    /**
     * 修改某个类别
     * @param category
     * @return
     */
    @Override
    public R categoryUpdate(Category category) {
        int i = categoryMapper.updateById(category);
        log.info("category update:",i);
        if(i == 0){
            R.fail("类别修改失败！");
        }
        return R.ok("类别修改成功！");
    }


    @Override
    public List<Category> adminlist() {
        List<Category> categoryList = categoryMapper.selectList(null);
        return categoryList;
    }
}
